package com.ljfiction.book.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.BookVolumeBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class MyBookvolemuActivity extends BaseActivity {

    @BindView(R.id.myvolume_back)
    ImageView myvolumeBack;
    @BindView(R.id.myvolume_recycle)
    RecyclerView myvolumeRecycle;
    private BaseQuickAdapter mAdapter;
    private List<BookVolumeBean.DataBean> volumeList = new ArrayList<>();
    private String bookId;
    private BookVolumeBean bookvolume;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bookvolemu);
        ButterKnife.bind(this);
        bookId = getIntent().getStringExtra("bookId");
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/books-getvolume")

                .params("bookId", bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson g = new Gson();
                        try {
                            bookvolume = g.fromJson(s, BookVolumeBean.class);
                            if (bookvolume.getReturnCode() == 200) {
                                volumeList.addAll(bookvolume.getData());
                                mAdapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void intView() {
        myvolumeRecycle.setAdapter(mAdapter = new BaseQuickAdapter<BookVolumeBean.DataBean, BaseViewHolder>(R.layout.myvolume_item, volumeList) {
            @Override
            protected void convert(BaseViewHolder helper, BookVolumeBean.DataBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.volumeitem, item.getVolumeName());
            }
        });
        myvolumeRecycle.setLayoutManager(new LinearLayoutManager(this));
        myvolumeRecycle.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (intent==null) {
                    intent = new Intent(MyBookvolemuActivity.this, MyBookChapterActivity.class);
                }
                intent.putExtra("volumeId",String.valueOf(volumeList.get(position).getId()));
                intent.putExtra("bookId",String.valueOf(volumeList.get(position).getBookid()));
                intent.putExtra("volumeName",volumeList.get(position).getVolumeName());
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.myvolume_back)
    public void onViewClicked() {
        goBack();
    }
}
